<?php


namespace app\system\service;

class VariableService
{
	public static function getlist($options)
	{
		$where = [];
		$where[] = ["appid", "=", $options["appid"]];
		if (isset($options["id"]) && $options["id"] != "") {
			$where[] = ["id", "=", $options["id"]];
		}
		if (isset($options["name"]) && $options["name"] != "") {
			$where[] = ["name", "like", "%" . $options["name"] . "%"];
		}
		$list = \think\Db::name("app_variable")->where($where)->order(["id" => "desc"])->page($options["page"], $options["limit"])->select();
		$count = \think\Db::name("app_variable")->where($where)->count();
		$pages = ceil($count / $options["limit"]);
		$data = [];
		foreach ($list as $item) {
			$data[] = self::get_array_info($item);
		}
		return ["code" => \app\system\model\Code::success, "data" => $data, "count" => $count, "pages" => $pages];
	}
	public static function get_array_info($item)
	{
		$item["contentEditConfig"] = json_decode($item["contentEditConfig"], true);
		$item["contentEditConfig"] = ["mode" => $item["contentEditConfig"]["mode"] ?? "default", "coder_language" => $item["contentEditConfig"]["coder_language"] ?? ""];
		$data = $item;
		return $data;
	}
	public static function getinfo($options)
	{
		$item = \think\Db::name("app_variable")->where(["id" => $options["id"]])->find();
		if (empty($item)) {
			return ["code" => \app\system\model\Code::fail, "msg" => "id不存在"];
		}
		return ["code" => \app\system\model\Code::success, "data" => self::get_array_info($item)];
	}
	public static function add($options)
	{
		if (\think\Db::name("app_variable")->where(["appid" => $options["appid"], "name" => $options["name"]])->count()) {
			return ["code" => \app\system\model\Code::fail, "msg" => "变量名已存在"];
		}
		$isSystemAuthUnpaid = isSystemAuthUnpaid();
		if ($isSystemAuthUnpaid && \think\Db::name("app_variable")->where(["appid" => $options["appid"]])->count() >= \app\common\model\FreeQuota::AppVariableQuota) {
			return ["code" => \app\system\model\Code::fail, "msg" => "当前授权为基础版,一个应用最多只能添加" . \app\common\model\FreeQuota::AppVariableQuota . "个变量" . \app\common\model\DefaultConfig::SystemAuthUnpaidSuffixText];
		}
		\think\Db::startTrans();
		try {
			\think\Db::name("app_variable")->insert(["appid" => $options["appid"], "name" => $options["name"], "content" => $options["content"], "verify_user" => $options["verify_user"], "create_time" => time(), "contentEditConfig" => json_encode($options["contentEditConfig"] ?? [], JSON_UNESCAPED_UNICODE)]);
			\think\Db::commit();
			return ["code" => \app\system\model\Code::success];
		} catch (\Exception $e) {
			\think\Db::rollback();
			return ["code" => \app\system\model\Code::error, "msg" => $e->getMessage()];
		}
	}
	public static function edit($options)
	{
		$name = \think\Db::name("app_variable")->where(["appid" => $options["appid"], "name" => $options["name"]])->find();
		if (!empty($name) && $name["id"] != $options["id"]) {
			return ["code" => \app\system\model\Code::fail, "msg" => "变量名已存在"];
		}
		\think\Db::startTrans();
		try {
			$row = \think\Db::name("app_variable")->where(["id" => $options["id"]])->find();
			if (empty($row)) {
				return ["code" => \app\system\model\Code::fail, "msg" => "id不存在"];
			}
			$data = ["name" => $options["name"], "content" => $options["content"], "verify_user" => $options["verify_user"], "update_time" => time(), "contentEditConfig" => json_encode($options["contentEditConfig"] ?? [], JSON_UNESCAPED_UNICODE)];
			\think\Db::name("app_variable")->where(["id" => $options["id"]])->update($data);
			\think\Db::commit();
			return ["code" => \app\system\model\Code::success];
		} catch (\Exception $e) {
			\think\Db::rollback();
			return ["code" => \app\system\model\Code::error, "msg" => $e->getMessage()];
		}
	}
	public static function del($options)
	{
		\think\Db::startTrans();
		try {
			$new = \think\Db::name("app_variable")->where(["id" => $options["id"]])->find();
			if (empty($new)) {
				return ["code" => \app\system\model\Code::fail, "msg" => "id不存在"];
			}
			\think\Db::name("app_variable")->where(["id" => $options["id"]])->delete();
			\think\Db::commit();
			return ["code" => \app\system\model\Code::success];
		} catch (\Exception $e) {
			\think\Db::rollback();
			return ["code" => \app\system\model\Code::error, "msg" => $e->getMessage()];
		}
	}
}